package com.fanshuai.springcloud.controller;

import com.fanshuai.springcloud.client.bookservice.BookServiceClient;
import com.fanshuai.springcloud.domain.Book;
import com.fanshuai.springcloud.service.BookService;
import com.fanshuai.springcloud.web.response.CallResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BookController implements BookServiceClient {
    @Resource(name = "baseBookService")
    private BookService bookService;

    @Value("${fanshuai.name}")
    String name;
    @Value("${fanshuai.age}")
    int age;

    @GetMapping("/book/list")
    @Override
    public CallResult<List<Book>> list() {
        return new CallResult<>(bookService.list());
    }

    @GetMapping("/book/get")
    @Override
    public CallResult<Book> one(long id) {
        return new CallResult<>(bookService.get(id));
    }

    @PostMapping("/book/add")
    @Override
    public CallResult<Boolean> add(@RequestBody Book book) {
        bookService.add(book);
        return new CallResult<>(true);
    }

    @PostMapping("/book/delete")
    @Override
    public CallResult<Boolean> delete(long id) {
        bookService.delete(id);
        return new CallResult<>(true);
    }

    @GetMapping("/config/get")
    public CallResult<Map<String, Object>> queryConfig() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("age", age);

        return new CallResult<>(map);
    }
}